from article.serializer import ArticleSerializer,CatagorySerializer
from article.models import Article,Catagory
from rest_framework import viewsets
from article.utils.pagination import ArticlePagination
from article.utils.permission import IsAdminUserOrReadOnly
from rest_framework.authentication import TokenAuthentication

class ArticleModleViewsets(viewsets.ModelViewSet):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all().order_by('id')
    pagination_class = ArticlePagination #设置分页
    permission_classes = [IsAdminUserOrReadOnly]
    
    #继承自ListCreateAPIView，在序列化数据真正保存之前调用，因此可以在这里添加额外的数据（即用户对象）
    #serializer是ArticleListSerializer序列化器实例，并且已经携带者验证后的数据，
    #save()方法可以接收关键字参数作为额外的需要保存的数据
    # def perform_create(self, serializer):
    #     serializer.save(author=self.request.user)
    
class CatagoryModleViewsets(viewsets.ModelViewSet):
    serializer_class = CatagorySerializer
    queryset = Catagory.objects.all().order_by('id')
    permission_classes = [IsAdminUserOrReadOnly]